/*
 * Created by JFormDesigner on Tue Nov 10 13:43:51 CET 2009
 */

package at.jku.pervasive.gui.jformdesigner;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;

import at.jku.pervasive.Application;

import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.FormSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.layout.Sizes;

/**
 * @author SHOCKIE
 */
public class PropertiesDialog extends JDialog
{
	public PropertiesDialog(Frame owner)
	{
		super(owner);
		initComponents();

		init();
	}

	public PropertiesDialog(Dialog owner)
	{
		super(owner);
		initComponents();

		init();
	}

	private void init()
	{
		final Application application = Application.getInstance();

		hostTextField.setText(application.getHost());
		portSpinner.setValue(application.getPort());

		okButton.setAction(new AbstractAction("OK")
		{
			public void actionPerformed(ActionEvent e)
			{
				application.stopListening();
				application.setHost(hostTextField.getText());
				application.setPort((Integer) portSpinner.getValue());
				application.startListening();

				dispose();
			}
		});

		cancelButton.setAction(new AbstractAction("Cancel")
		{
			public void actionPerformed(ActionEvent e)
			{
				dispose();
			}
		});
	}

	private void initComponents()
	{
		// JFormDesigner - Component initialization - DO NOT MODIFY
		// //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Open Source Project license - unknown
		dialogPane = new JPanel();
		contentPanel = new JPanel();
		JLabel hostLabel = new JLabel();
		hostTextField = new JTextField();
		JLabel portLabel = new JLabel();
		portSpinner = new JSpinner();
		buttonBar = new JPanel();
		okButton = new JButton();
		cancelButton = new JButton();
		CellConstraints cc = new CellConstraints();

		// ======== this ========
		setModal(true);
		setResizable(false);
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

		// ======== dialogPane ========
		{
			dialogPane.setBorder(Borders.DIALOG_BORDER);
			dialogPane.setLayout(new BorderLayout());

			// ======== contentPanel ========
			{
				contentPanel.setLayout(new FormLayout(new ColumnSpec[] {
						new ColumnSpec(ColumnSpec.RIGHT, Sizes.DEFAULT, FormSpec.NO_GROW),
						FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
						new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW) }, new RowSpec[] {
						FormFactory.DEFAULT_ROWSPEC, FormFactory.LINE_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC }));

				// ---- hostLabel ----
				hostLabel.setText("Host");
				contentPanel.add(hostLabel, cc.xy(1, 1));

				// ---- hostTextField ----
				hostTextField.setText("192.168.0.10");
				contentPanel.add(hostTextField, cc.xywh(3, 1, 1, 1, CellConstraints.FILL, CellConstraints.DEFAULT));

				// ---- portLabel ----
				portLabel.setText("Port");
				contentPanel.add(portLabel, cc.xy(1, 3));

				// ---- portSpinner ----
				portSpinner.setModel(new SpinnerNumberModel(6666, 1024, 65535, 1));
				contentPanel.add(portSpinner, cc.xywh(3, 3, 1, 1, CellConstraints.LEFT, CellConstraints.DEFAULT));
			}
			dialogPane.add(contentPanel, BorderLayout.CENTER);

			// ======== buttonBar ========
			{
				buttonBar.setBorder(Borders.BUTTON_BAR_GAP_BORDER);
				buttonBar.setLayout(new FormLayout(new ColumnSpec[] { FormFactory.GLUE_COLSPEC,
						FormFactory.BUTTON_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, FormFactory.BUTTON_COLSPEC },
						RowSpec.decodeSpecs("pref")));

				// ---- okButton ----
				okButton.setText("OK");
				buttonBar.add(okButton, cc.xy(2, 1));

				// ---- cancelButton ----
				cancelButton.setText("Cancel");
				buttonBar.add(cancelButton, cc.xy(4, 1));
			}
			dialogPane.add(buttonBar, BorderLayout.SOUTH);
		}
		contentPane.add(dialogPane, BorderLayout.CENTER);
		pack();
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization
		// //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY
	// //GEN-BEGIN:variables
	// Generated using JFormDesigner Open Source Project license - unknown
	private JPanel dialogPane;
	private JPanel contentPanel;
	private JTextField hostTextField;
	private JSpinner portSpinner;
	private JPanel buttonBar;
	private JButton okButton;
	private JButton cancelButton;
	// JFormDesigner - End of variables declaration //GEN-END:variables
}
